<?php
/**
 * Created by PhpStorm.
 * User: wudiying
 * Date: 2020/2/19
 * Time: 下午9:55
 */


/**
 * swoole之异步mysql操作
 * 语法：
 * swoole_mysql
 * connect
 * on
 * escape
 * query
 * 示例：
 *
 */

$db = new swoole_mysql();
$config = [
    'host' => '47.107.63.90',
    'user' => 'root',
    'password' => 'wdyname123',
    'database' => 'bj18',
    'charset' => 'utf8'
];


//连接数据
$db->connect($config, function ($db, $result) {
    if ($result === false) {
        var_dump($db->connect_errno, $db->connect_error);
        die('失败');
    }
//成功
    $sql = 'show tables';
    $db->query($sql, function (swoole_mysql $db, $res) {
        if ($res === false) {
            var_dump($db->error);  // 这个时候就不是db的错误了
            die('查询失败');
        } else {
            if ($res === true) {
                var_dump($db->affected_rows, $db->insert_id);
            }
        }
        var_dump($res);
        $db->close();
    });
});